import 'dart:io';

import 'package:clean_app/interface/mid_request.dart';
import 'package:dio/dio.dart';

//新增
class AiApi {
  Future<Map<String, dynamic>> classification({required File file}) async {
    final res = await XHttp().postWithGlobalToken(
      url: "/v1/app/ai/classification",
      isRaw: false,
      param: {
        "file": await MultipartFile.fromFile(file.path,
            filename: file.path.split('/').last),
      },
    );
    return Map<String, dynamic>.from(res ?? {});
  }

  Future<Map<String, dynamic>> scoring({required File file}) async {
    final res = await XHttp().postWithGlobalToken(
      url: "/v1/app/ai/scoring",
      isRaw: false,
      param: {
        "file": await MultipartFile.fromFile(file.path,
            filename: file.path.split('/').last),
      },
    );
    return Map<String, dynamic>.from(res ?? {});
  }
}



class AuthApi {
  appUsersMe() async {
    return await XHttp().getWithGlobalToken(url: "/v1/app/users/me");
  }

  authRegister(Map<String, dynamic> m) async {
    return await XHttp().post(url: "/v1/app/auth/register", param: m);
  }

  authLogin(Map<String, dynamic> m) async {
    return await XHttp().post(url: "/v1/auth/login", param: m);
  }
}

class TaskApi {
  //  /v1/tags
  getTags() async {
    return await XHttp().getWithGlobalToken(url: "/v1/tags");
  }

  // /v1/app/submissions
  getSubmissions() async {
    return await XHttp().getWithGlobalToken(url: "/v1/app/submissions");
  }

  // /v1/app/submissions/{submission_id}/annotations
  putSubmissions(var submissionID, Map<String, dynamic> m) async {
    return await XHttp().putWithGlobalToken(
      url: "/v1/app/submissions/$submissionID/annotations",
      isRaw: true,
      param: m,
    );
  }

  // /v1/app/submissions
  postSubmissions(Map<String, dynamic> m) async {
    return await XHttp().postWithGlobalToken(
      url: "/v1/app/submissions",
      isRaw: true,
      param: m,
    );
  }

  // /v1/all_scenes
  getAllScenes() async {
    return await XHttp().getWithGlobalToken(url: "/v1/all_scenes");
  }
}

class UploadApi {
  // /v1/cos/put-presigned-url
  getPresignedUrl() async {
    return await XHttp().getWithGlobalToken(url: "/v1/cos/put-presigned-url");
  }

  uploadWithPresignedUrl(String filePath, String presignedUrl,
      ProgressCallback progressCallback) async {
    File file = File(filePath);
    if (!await file.exists()) {
      throw Exception("文件不存在: $filePath");
    }

    Options options = Options(
      method: 'PUT',
      headers: {
        'Content-Type': 'application/octet-stream',
      },
    );

    try {
      Dio dio = Dio();
      Response response = await dio.put(
        presignedUrl,
        data: file.openRead(),
        options: options,
        onSendProgress: (int sent, int total) {
          double progress = sent / total;
          print('Progress: ${progress.toStringAsFixed(2)}');
          progressCallback(sent, total);
        },
      );
      if (response.statusCode == 200) {
        print('上传成功');
      } else {
        throw Exception("上传失败 ${response.statusMessage}");
      }
    } catch (error) {
      print('Error: $error');
      throw Exception("上传失败 ${error.toString()}");
    }
  }
}

